package com.rykj.qxj.test.xtest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class Jdbc {

	// 定义sql
	private static String sql = "SELECT * FROM users t WHERE t.username LIKE ?";
	 private  static  String sql1="select table_name from information_schema.tables where table_schema=?"; 
	public static void main(String[] args) {
		// 连接对象
		Connection connection = null;
		// 预编译对象 PreparedStatement
		java.sql.PreparedStatement preparedStatement = null;
		// 结果集
		ResultSet resultSet = null;
		try {
		// 加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		// 获取连接对象
		connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/flight_bigdata", "root", "123");
		// 获取preparedStatement
		//preparedStatement = connection.prepareStatement(sql);
		preparedStatement=connection.prepareStatement(sql1);
		// 为preparedStatement对象的sql中的占位符设置参数
		preparedStatement.setString(1, "flight_bigdata");
		// 执行sql语句，并返回结果集到resultSet中
		resultSet = preparedStatement.executeQuery();
		// 遍历结果集
		//List<String> resultList=new ArrayList<String>(); 
		Set<String> resultSets=new HashSet<String>();
		while (resultSet.next()) {
		String tableName = resultSet.getString("table_name");
		resultSets.add(tableName);
		}
		for (String string : resultSets) {
			System.out.println(string);
			
		}
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		// 释放资源
		if (resultSet != null) {
		try {
			resultSet.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		if (preparedStatement != null) {
		try {
			preparedStatement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		if (connection != null) {
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		}
	}

}
